<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:replace="fragments/custom_base::commonHead"></th:block>
    <th:block th:replace="fragments/custom_link::pear-css"></th:block>
    <link rel="stylesheet" href="/css/other/form.css" />
    <style>

    </style>
</head>
<body>
<div class="formBox">
    <form action="" class="layui-form">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <input type="hidden" name="id" th:value="${sysRule.id}">
                    <div class="layui-form-item">
                        <label class="layui-form-label">权限名称</label>
                        <div class="layui-input-block">
                            <input autocomplete="off" class="layui-input" lay-verify="required" name="name" placeholder="权限名称" th:value="${sysRule.name}"
                                   type="text">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">权限描述</label>
                        <div class="layui-input-block">
                            <textarea name="description" placeholder="权限描述"  class="layui-textarea" maxlength="255">[[${sysRule.description}]]</textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">权限范围</label>
                        <div class="layui-input-block">
                            <ul class="dtree" data-id="-1" id="getRoleDept" name="rules"></ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="formBoxBottom">
            <div class="button-container">
                <button class="layui-btn layui-btn-primary layui-btn-sm" lay-filter="rules-update" lay-submit="" type="submit">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button class="layui-btn layui-btn-sm" type="reset">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</div>


<th:block th:replace="fragments/custom_link::pear-js"></th:block>
<script th:inline="javascript">
    layui.use(['form', 'jquery', 'dtree'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;
        let MODULE_PATH = "/admin/rules/";
        let ruleId = [[${sysRule.id}]];
        dtree.render({
            elem: "#getRoleDept",
            url: MODULE_PATH + "get_role_dept?id="+ruleId,
            method: 'get',
            checkbar: true,
            checkbarType: "all",
            initLevel:99,
            dataFormat: "levelRelationship",
            response: {treeId: "id", parentId: "pid", title: "title"}
        });

        form.on('submit(rules-update)', function (data) {
            let param = dtree.getCheckbarNodesParam("getRoleDept");
            let ids = '';
            for (let i = 0; i < param.length; i++) {
                let id = param[i].nodeId;
                ids += id + ',';
            }
            ids = ids.substr(0, ids.length - 1);
            data.field.rules = ids;
            $.ajax({
                url: MODULE_PATH+'update',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.status==200) {
                        layer.msg(result.message, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("SysRulesTable", {
                                where: {},
                            });
                        });
                    } else {
                        layer.msg(result.message, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>